<?php

declare(strict_types=1);

namespace app\common\model;

class UserActivity extends Base
{

    protected $name = 'user_activity';

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'uid')->field('id,code,nickname,mobile');
    }
    /**
     * 提交报名
     */
    public static function submit($param, $user)
    {
        try {
            $time = time();
            $av = Activity::where(['id' => $param['id']])->find();
            if (empty($av)) {
                return ['code' => -1, 'msg' => '活动不存在'];
            }
            if ($av['bm_s_time'] > $time) {
                return ['code' => -1, 'msg' => '活动报名还未开始！'];
            }
            if ($av['bm_e_time'] < $time) {
                return ['code' => -1, 'msg' => '活动报名已结束！'];
            }
            $info = parent::where(['uid' => $user['id'], 'activity_id' => $param['id']])->find();
            if (!empty($info)) {
                return ['code' => -1, 'msg' => '您已报名该活动！'];
            }
            $data['uid'] = $user['id'];
            $data['activity_id'] = $param['id'];
            $data['rname'] = $param['rname'];
            $data['mobile'] = $param['mobile'];
            $data['note'] = $param['note'];
            parent::create($data);
            return ['code' => 1, 'msg' => '报名成功！'];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
